import { find, Label, Node, Prefab, _decorator } from 'cc';
import { Const } from '../Constans/Const';
import GD from '../Constans/GD';
import BaseView from '../UI/BaseView';
import { PoolManager } from '../Managers/PoolManager';
import { UiManager } from '../Managers/UiManager';
import { UtilPub } from '../Utilities/UtilPub';
import { tyqSDK } from '../Managers/TyqSDK';
import { composeModel } from '../Gameplay/ComposeModel';
import { userData } from '../Data/UserData';
import { UtilScene } from '../Utilities/UtilScene';
import { handIndexs } from '../Constans/HandData';
import { BuildListItem } from '../UI/BuildListItem';
const { ccclass, property } = _decorator;

@ccclass('BuildList')
export class BuildList extends BaseView {
    get titleLabel() { return find("root/window/titleLabel", this.node)!.getComponent(Label)! }
    get topLabel() { return find("root/window/top/star/topLabel", this.node)!.getComponent(Label)! }
    get closeBtn() { return find("root/window/btnClose", this.node)! }
    get starLabel() { return find("root/window/top/star/btnBg/starLayout/starLabel", this.node)!.getComponent(Label)! }
    get buildListLayout() { return find("root/window/sview/mask/build_list", this.node)! }

    items: Node[] = []

    onLoad() {
        this.on(GD.event.chgGreenStar, this.chgGreenStar, this)
        this.bindButton(this.closeBtn, () => {
            this.close()
        })
    }

    chgGreenStar() {
        this.starLabel.string = userData.greenStar + ""
    }

    show(args: any) {
        super.show(args)
        this.reload()
        tyqSDK.eventSendCustomEvent("查看建造清单")
    }

    reload() {
        this.chgGreenStar()
        PoolManager.instance.putNodeArr(this.items)
        let needShowRooms = UtilScene.getNeedShowStarRooms()
        //生成对应的对象
        UtilPub.getPrefab(Const.Prefabs.build_list_region, (p: Prefab) => {
            needShowRooms.forEach((row: any) => {
                let item = PoolManager.instance.getNode(p, this.buildListLayout)!
                item.getComponent(BuildListItem)!.init(row.id)
                this.items.push(item)
            })

            let handIndex = composeModel.getHandIndex();
            if (handIndex == handIndexs.btnBuild) {
                composeModel.addHandIndex();
                let obj: any = {};
                obj.id = composeModel.getHandIndex();
                let btnGo = find("root/mid/goBtn", this.buildListLayout.children[0]);
                obj.node = btnGo;
                obj.delayTime = 0.5;
                UiManager.instance.showDialog(Const.Dialogs.HandLayer, obj);
                composeModel.addHandIndex();
            }
        })
    }




}


